package com.lincoln.cloud.serverclient.interfaces;

import com.lincoln.cloud.common.bean.CommonServerInfo;
import com.lincoln.cloud.common.bean.IpAndPort;
import com.lincoln.cloud.common.utils.ConfigUtil;
import com.lincoln.cloud.serverclient.abstracter.NettyCommunicationProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * server-client上下文
 *
 * @author lincoln
 */
public class ServerClientContext {
    private static final Logger logger = LoggerFactory.getLogger(ServerClientContext.class);
    /**
     * 本服务信息
     */
    private static CommonServerInfo commonServerInfo;
    private static Properties properties;
    private static CommunicationProtocol communicationProtocol;
    private static HeartBeatThread heartBeatThread;

    public static boolean init() {
        if (communicationProtocol() == null) {
            communicationProtocol(NettyCommunicationProtocol.class);
        }
        loadProperties();
        commonServerInfo = new CommonServerInfo();
        commonServerInfo.setServerName(properties.getProperty("server-name"));
        commonServerInfo.setIntroduce(properties.getProperty("introduce"));
        commonServerInfo.setIpAndPort(new IpAndPort(properties.getProperty("ip"), Integer.parseInt(properties.getProperty("port"))));
        commonServerInfo.setDeviceType(Integer.parseInt(properties.getProperty("device-type")));
        commonServerInfo.setProtocolType(Integer.parseInt(properties.getProperty("protocol-type")));
        try {
            if (communicationProtocol().init() && communicationProtocol().login()) {
                logger.info("登陆成功!");
                heartBeatThread = new HeartBeatThread();
                heartBeatThread.start();
                return true;
            } else {
                logger.info("登陆失败!");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取本机信息
     *
     * @return
     */
    public static CommonServerInfo commonServerInfo() {
        return commonServerInfo;
    }

    /**
     * 设置本机信息
     *
     * @return
     */
    public static void commonServerInfo(CommonServerInfo commonServerInfo) {
        ServerClientContext.commonServerInfo = commonServerInfo;
    }

    /**
     * 获取协议
     *
     * @return
     */
    public static CommunicationProtocol communicationProtocol() {
        return communicationProtocol;
    }

    /**
     * 设置协议
     *
     * @param clazz
     */
    public static void communicationProtocol(Class<? extends CommunicationProtocol> clazz) {
        try {
            communicationProtocol = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置协议
     *
     * @param communicationProtocol
     */
    public static void communicationProtocol(CommunicationProtocol communicationProtocol) {
        ServerClientContext.communicationProtocol = communicationProtocol;
    }

    /**
     * 加载配置
     *
     * @return
     */
    public static Properties loadProperties() {
        if (properties == null) {
            reloadProperties();
        }
        return properties;
    }

    /**
     * 重加载配置
     *
     * @return
     */
    public static Properties reloadProperties() {
        properties = ConfigUtil.loadProperties("default-application.properties");
        ConfigUtil.loadProperties("application.properties", properties);
        return properties;
    }
}
